/* $Id: ContestProblemEntry.java 137 2011-08-29 20:33:36Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.datatypes;

import java.io.Serializable;
import javax.cache.Cache;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import alt.djudge.frontend.server.models.CacheModel;
import alt.djudge.frontend.server.utils.Parsers;
import alt.djudge.frontend.shared.dto.admin.ContestProblemEntryDTO;
import alt.djudge.frontend.shared.dto.admin.AdminDTOEntry;


@PersistenceCapable(detachable="true")
public class ContestProblemEntry extends AbstractEntry implements Serializable
{
	private static final long serialVersionUID = 1L;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	protected Long id;
	
	@Override
	public Long getId()
	{
		return id;
	}
	
	@Override
	public void setId(Long id)
	{
		this.id = id;
	}
	
	@Persistent
	private Long contestId;
	
	@Persistent
	private Long problemId;
		
	@Persistent
	private Long userId = 1L;
	
	@Persistent
	private String name = "-";
	
	// short name
	@Persistent
	private String sid = "-";
	
	public ContestProblemEntry()
	{
		// TODO Auto-generated constructor stub
	}
	
	public ContestProblemEntry(Element next)
	{
		readXml(next);
	}

	public String getSid()
	{
		return sid;
	}

	public void setSid(String sid)
	{
		this.sid = sid;
	}
	
	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}
	
	public void setUserId(Long userId)
	{
		this.userId = userId;
	}

	public Long getUserId()
	{
		return userId;
	}

	public void setContestId(Long contestId)
	{
		this.contestId = contestId;
	}


	public Long getContestId()
	{
		return contestId;
	}


	public void setProblemId(Long problemId)
	{
		this.problemId = problemId;
	}


	public Long getProblemId()
	{
		return problemId;
	}
	
	@Override
	public String toString()
	{
		return sid + ": " + name;
	}


	@Override
	protected void clearCacheCustom()
	{
		// FIXME - for all contest users
		// String key = CacheModel.getProblemUserMapEntries(contestId, userId);
		// Cache cache = CacheModel.getStaticCache();
		
		String key = CacheModel.getContestProblemMapEntries(contestId);
		Cache cache = CacheModel.getStaticCache();
		cache.get(key);
	}

	public Document getXml()
	{
		Document doc = DocumentHelper.createDocument();
		Element elem = doc.addElement("Problem");
		
		elem.addAttribute("id", "" + getId());
		elem.addAttribute("sid", "" + sid);
		elem.addAttribute("name", "" + name);
		elem.addAttribute("active", "" + active);
		elem.addAttribute("contestId", "" + contestId);
		elem.addAttribute("problemId", "" + problemId);

		return doc;
	}
	
	private void readXml(Element el)
	{
		sid = el.attributeValue("sid");
		name = el.attributeValue("name");
		active = Parsers.parseBoolean(el.attributeValue("active"), true);
		problemId = Parsers.parseLong(el.attributeValue("problemId"));
		contestId = Parsers.parseLong(el.attributeValue("contestId"));
	}

	@Override
	public void fromDTO(AdminDTOEntry dtoEntry)
	{
		setContestId(Long.parseLong(dtoEntry.getFieldValue(1)));
		setProblemId(Long.parseLong(dtoEntry.getFieldValue(2)));
		setSid(dtoEntry.getFieldValue(3));
		setName(dtoEntry.getFieldValue(4));
	}

	@Override
	public AdminDTOEntry ownAdminDTO()
	{
		String[] data = new String[] {
			getId().toString(),
			getContestId().toString(),
			getProblemId().toString(),
			getSid(),
			getName(),
		};
		return new ContestProblemEntryDTO(data);
	}
}
